package robotleMvc.ui.lizhi.uvMove
{
	import _Pan3D.display3D.Display3DSprite;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	
	public class UvMoveDisplaySprite extends Display3DSprite
	{
		protected var lizhiVertexBuffer:VertexBuffer3D;
		protected var lizhiStateVertexBuffer:VertexBuffer3D;
		protected var lizhiIndexBuffer:IndexBuffer3D;
		protected var _program3D:Program3D;
		protected var _timer:Number=0;
		protected var _uvSeep:int=29;
		public function UvMoveDisplaySprite(context:Context3D)
		{
			super(context);
		}
		override public function update() : void {
			if (!this._visible||_timer>(_uvSeep-2)) {
				return;
			}
			if (_objData && _objData.texture) {
				if(_program3D){
					_context.setProgram(_program3D);
				}else{
					_context.setProgram(this.program);
				}
				setVc();
				setVa();
				resetVa();
			}
		}
		protected function initLizhiData():void
		{
			var context3D:Context3D=_context;
			var _triNum:Number=360/2;
			_triNum=1
			var _pArr:Array=new Array;
			var _iArr:Array=new Array;
			var _w:Number=100;
			var _h:Number=100;
			for(var i:int=0;i<_triNum;i++){
				var zz:Number=0;
				_pArr.push(-_w, _h, zz, 0,0, 1);
				_pArr.push(_w, _h, zz, 1/_uvSeep,0, 1);
				_pArr.push(_w, -_h, zz, 1/_uvSeep,1, 1);
				_pArr.push(-_w, -_h, zz, 0,1, 1);
				var n:int=i*4;
				_iArr.push(n+0,n+1,n+2);
				_iArr.push(n+0,n+2,n+3);
			}
			lizhiVertexBuffer  = context3D.createVertexBuffer( _triNum*4, 6);
			lizhiVertexBuffer.uploadFromVector(Vector.<Number>(_pArr), 0, _pArr.length/6);
			lizhiIndexBuffer  = context3D.createIndexBuffer(_iArr.length);
			lizhiIndexBuffer.uploadFromVector(Vector.<uint>(_iArr), 0, _iArr.length);
		}
		override protected function setVa() : void {
			if(!lizhiIndexBuffer){
				initLizhiData();
				return;
			}
			_timer++;
			_context.setVertexBufferAt(0,lizhiVertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1,lizhiVertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,Vector.<Number>( [_timer/_uvSeep,1,1,1])); 
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(lizhiIndexBuffer, 0, -1);
		}
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setTextureAt(0,null);
		}
		override protected function setVc() : void {
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
		}
		public function get program3D():Program3D
		{
			return _program3D;
		}
		public function set program3D(value:Program3D):void
		{
			_program3D = value;
		}
		public function set play(temp:Boolean):void{
			_timer=temp?0:99;
			
		}
		
	}
}